package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.system.LoginVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
public class UserInfoApiController {

    private final UserInfoService userInfoService;

    public UserInfoApiController(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @Operation(summary = "账号密码登录接口")
    @Parameters({
            @Parameter(name = "loginVo", description = "登录对象", required = true)
    })
    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody LoginVo loginVo) {
        Map<String, String> tokenMap = this.userInfoService.userLogin(loginVo);
        return Result.ok(tokenMap);
    }

    // /api/user/userInfo/getUserInfoVo/{userId}
    @Operation(summary = "根据用户ID查询用户信息")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID", required = true)
    })
    @GetMapping("/userInfo/getUserInfoVo/{userId}")
    public Result<UserInfoVo> getUserInfoVoByUserId(@PathVariable Long userId) {
        UserInfoVo userInfoVo = this.userInfoService.getUserInfoVoByUserId(userId);
        return Result.ok(userInfoVo);
    }

    /**
     * 该接口提供给给专辑服务，展示声音列表动态判断付费标识
     *
     * @return checkResult:{声音ID：购买结果}   结果：1（已购）0（未购买）
     */
    @Operation(summary = "判断当前用户某一页中声音列表购买情况")
    @Parameters({
            @Parameter(name = "userId", description = "用户ID", required = true),
            @Parameter(name = "albumId", description = "专辑ID", required = true),
            @Parameter(name = "trackIdList", description = "待检查购买情况声音列表", required = true)
    })
    @PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
    public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long userId,
                                                      @PathVariable Long albumId,
                                                      @RequestBody List<Long> trackIdList) {

        Map<Long, Integer> checkResult = this.userInfoService.userIsPaidTrack(userId, albumId, trackIdList);
        return Result.ok(checkResult);
    }


    @Operation(summary = "判断当前用户是否订阅该专辑")
    @RequiresLogin(required = false)
    @GetMapping("/userInfo/isSubscribe/{albumId}")
    public Result<Boolean> isSubscribe(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean isSubscribe = this.userInfoService.isSubscribe(albumId, userId);
        return Result.ok(isSubscribe);
    }

    @Operation(summary = "判断用户是否购买过指定专辑")
    @RequiresLogin
    @GetMapping("/userInfo/isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        Boolean isPaidAlbum = this.userInfoService.isPaidAlbum(albumId, userId);
        return Result.ok(isPaidAlbum);
    }

    @Operation(summary = "根据专辑id+用户ID获取用户已购买声音id列表")
    @RequiresLogin
    @GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
    public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        List<Long> paidTrackIdList = this.userInfoService.findUserPaidTrackList(albumId, userId);
        return Result.ok(paidTrackIdList);
    }

    @Operation(summary = "处理用户购买记录（虚拟物品发货）")
    @RequiresLogin
    @PostMapping("/userInfo/savePaidRecord")
    public Result<Void> savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo) {
        Long userId = AuthContextHolder.getUserId();
        if (userId != null) {
            userPaidRecordVo.setUserId(userId);
        }
        userInfoService.savePaidRecord(userPaidRecordVo);
        return Result.ok();
    }


    @Operation(summary = "更新Vip到期失效状态")
    @GetMapping("updateVipExpireStatus")
    public Result<Void> updateVipExpireStatus() {
        userInfoService.updateVipExpireStatus();
        return Result.ok();
    }

}

